package com.dongdongshop.service.impl.item;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.order_item.TbOrderItem;
import com.dongdongshop.service.item.ItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public List<TbItem> listItemByGoodsId(Long goodsId) {
        return tbItemMapper.listItemByGoodsId(goodsId);
    }

    @Override
    public TbItem getItemById(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public void updateToNum(List<TbOrderItem> orderItemList) {
        for (TbOrderItem orderItem : orderItemList) {
            TbItem item = tbItemMapper.selectByPrimaryKey(orderItem.getItemId());
            Integer stockCount = item.getStockCount();
            Integer num = orderItem.getNum();
            int i = stockCount-num;
            item.setStockCount(i);
            tbItemMapper.updateByPrimaryKeySelective(item);
        }
    }
}
